package controller

import (
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	teamones_helper "github.com/teamones-open/go-helper"
)

// 插入日志
func LogInsert(ctx *gin.Context) {
	var addLogs validate.AddLogs

	err := ctx.ShouldBindJSON(&addLogs)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	for i := range addLogs.Data {
		resData, createErr := service.CreateLog(addLogs.Data[i])
		if createErr != nil {
			// 插入失败
			response.Fail(ctx, nil, createErr.Error())
			return
		} else {
			// 插入数据成功
			addLogs.Data[i].Id = resData.Id
			addLogs.Data[i].Created = resData.Created
			addLogs.Data[i].Uuid = resData.Uuid
		}
	}

	response.Success(ctx, gin.H{"data": addLogs.Data}, "success")
}

// 查询单条日志
func LogFind(ctx *gin.Context) {
	var selectLogQuery validate.SelectLogQuery

	err := ctx.ShouldBindJSON(&selectLogQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := teamones_helper.GenerateSelectQueryParam(selectLogQuery.Param, "log")

	resData, queryErr := service.FindLog(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 查询多条日志
func LogSelect(ctx *gin.Context) {
	var selectLogQuery validate.SelectLogQuery

	err := ctx.ShouldBindJSON(&selectLogQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := teamones_helper.GenerateSelectQueryParam(selectLogQuery.Param, "log")

	total, resData, queryErr := service.SelectLog(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 写入交易记录
func WriteTransactionRecords(ctx *gin.Context) {
	var addTransactionRecords validate.AddTransactionRecords

	err := ctx.ShouldBindJSON(&addTransactionRecords)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.WriteTransactionRecords(addTransactionRecords)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

// 记录monolog日志
func WriteMonologRecords(ctx *gin.Context) {
	var addMonolog validate.AddMonolog

	err := ctx.ShouldBindJSON(&addMonolog)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.WriteMonologRecords(addMonolog)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

// 查询monolog日志
func SelectMonologRecords(ctx *gin.Context) {
	var selectMonologQuery validate.SelectMonolog

	err := ctx.ShouldBindJSON(&selectMonologQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := teamones_helper.GenerateSelectQueryParam(selectMonologQuery.Param, "log")

	total, resData, queryErr := service.SelectMonoLog(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// WriteRequestRecords 记录monolog日志
func WriteRequestRecords(ctx *gin.Context) {

	var addMonolog []validate.RequestLog

	err := ctx.ShouldBindJSON(&addMonolog)

	if err != nil {
		logrus.Errorln("WriteRequestRecords :", err.Error())
		response.Fail(ctx, nil, err.Error())
		return
	}

	createErr := service.WriteRequestRecords(addMonolog)
	if createErr != nil {
		// 插入失败
		logrus.Errorln(err.Error())
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": nil}, "success")
}
